<?php


namespace api\user\controller;


use api\user\model\UserModel;
use api\user\model\SmsCodeModel;
use api\user\validate\UserValidate;
use cmf\controller\RestBaseController;
use think\facade\Filesystem;
use think\Request;

class UserController extends RestBaseController
{
    //注册api
    public function register(Request $request)
    {
        $data = $request->param();
        //验证器
        $validate = new UserValidate();
        $val = $validate->scene('register')->check($data);
        if(!$val)
        {
            $this->error($validate->getError());
        }

        //验证验证码
        $c = CheckSmsCode($data['mobile'], $data['code']);
        if (!$c){
            $this->error('验证码错误');
        }
        //入库
        $res = UserModel::create($data);
        if($res)
        {
            $this->success('注册成功');
        }else{
            $this->error('注册失败');
        }
    }
    //登录api
    public function login(Request $request)
    {
        $mobile = $request->param('mobile');
        $code = $request->param('code');

        if(empty($mobile))
        {
            $this->error('手机号码不能为空');
        }
        if(empty($code)){
            $this->error('验证码不能为空');
        }
        $res = UserModel::where('mobile','=',$mobile)
            ->find();
        if(empty($res))
        {
            $this->error('手机号错误');
        }
        $sms = SmsCodeModel::where('mobile','=',$mobile)
            ->find();
        $c = checkSmsCode($mobile, $code);
        if (!$c){
            $this->error('验证码错误');
        }
            $token = sendToken($res['id'],$mobile);
            $this->success(
                '登录成功',
                [
                    'data'=>$token
                ]
            );

    }
    public function SentCode()
    {
        $mobile = input('post.mobile');

        //发送验证码
        sendAliSms($mobile);
    }
    //上传文件
    public function UploadImage(Request $request)
    {
        $image = $request->file('image');
        $savename = Filesystem::disk('public')->putFile( 'user', $image);
        if ($savename){
            $this->success('上传成功',[
                'data' => $savename
            ]);
        }else{
            $this->error('上传失败');
        }
    }
    //查询用户数据
    public function userList()
    {
        $id = input('id/d');
        if (!$id) {
            $this->error('参数错误');
        }
        $data = UserModel::field('id,image,nickname,mobile')->find($id);
        if (!$data){
            $this->error('无用户');
        }
        $this->success('查询成功',[
            'data' => $data
        ]);

    }
    public function edit()
    {
        $data = input('post.');
        if (!$data){
            $this->error('参数错误');
        }
        $validate = new UserValidate();
        $res = $validate->scene('edit')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        if (!$data['id']){
            $this->error('ID错误');
        }
        $user = UserModel::find($data['id']);
        $user->image = $data['image'];
        $user->nickname = $data['nickname'];
        $user->mobile = $data['mobile'];
        $user->update_time = time();
        if ($user->save()){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }

    }
}
